package com.pet.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class ResourceUtil {
	private static Map<String, Properties> map = new HashMap<String, Properties>();
	public static Map<String, Properties> getResourceMap(){
		return map;
	}
	
	public static void initResourceMap(String path){
		 readItemField(path, "properties", map);
	}
	
	/**
     * 资源文件对象取得.
     * @param path 文件路径
     * @param extension 文件后缀
     * @param ap 资源文件对象
     * @throws RichClientWebException RichClientWebException
     */
	 private static void readItemField(String path, String extension, Map<String, Properties> retMap) {
        final File dir = new File(path);
        if (!dir.isDirectory()) {
            return;
        }
        final File[] dirs = dir.listFiles();
        try {
            for (int i = 0; i < dirs.length; i++) {
                if (dirs[i].isDirectory()) {
                    final File[] files = dirs[i].listFiles();
                    for (File file : files) {
                        addProperties(retMap, file, extension);
                    }
                } else if (dirs[i].isFile()) {
                    addProperties(retMap, dirs[i], extension);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    private static void addProperties(Map<String, Properties> retMap, File file, String extension) throws IOException {
	    if (file.getName().endsWith(extension)) {
	        final Properties properties = new Properties();
	        properties.load(new FileInputStream(file));
	        retMap.putAll((Map) properties);
	    }
	}
}
